import 'package:flutter/material.dart';
import 'package:hospitable_rental/routes/routes.dart';
import 'package:hospitable_rental/utils/common_toast.dart';
import 'package:hospitable_rental/viewmodels/auth_viewmodel.dart';
import 'package:provider/provider.dart';

class ProfileHeader extends StatelessWidget {
  const ProfileHeader({Key? key}) : super(key: key);

  Widget _notLoginBuilder(BuildContext context) {
    return Container(
      height: 95.0,
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          Container(
            width: 65.0,
            height: 65.0,
            margin: EdgeInsets.symmetric(horizontal: 15.0),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                "https://tva1.sinaimg.cn/large/006y8mN6ly1g6tbgbqv2nj30i20i2wen.jpg",
              ),
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              GestureDetector(
                onTap: () => Navigator.of(context).pushNamed(Routes.login),
                child: Text(
                  "登录/注册",
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                ),
              ),
              Text(
                "登录后可以体验更多",
                style: TextStyle(color: Colors.white),
              ),
            ],
          )
        ],
      ),
    );
  }

  Widget _loginBuilder(BuildContext context) {
    var userInfo = context.watch<AuthViewModel>().userInfo;
    return Container(
      height: 95.0,
      decoration: BoxDecoration(color: Colors.green),
      child: Row(
        children: [
          Container(
            width: 65.0,
            height: 65.0,
            margin: EdgeInsets.symmetric(horizontal: 15.0),
            child: CircleAvatar(
              backgroundImage: NetworkImage(
                userInfo.avatar ?? "https://tva1.sinaimg.cn/large/006y8mN6ly1g6tbnovh8jj30hr0hrq3l.jpg",
              ),
            ),
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              GestureDetector(
                onTap: () => CommonToast.showToast("跳转个人详情页"),
                child: Text(
                  userInfo.nickname ?? "已登录的用户名",
                  style: TextStyle(color: Colors.white, fontSize: 20.0),
                ),
              ),
              Text(
                "查看编辑个人资料",
                style: TextStyle(color: Colors.white),
              ),
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    bool isLogin = context.read<AuthViewModel>().isLogin;
    return isLogin ? _loginBuilder(context) : _notLoginBuilder(context);
  }
}
